
//
//  LearningTaskController.m
//  PASystem
//
//  Created by 刘沿 on 16/9/23.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "LearningTaskController.h"
#import "TaskModel.h"
#import "TaskViewCell.h"
#import "TaskDetailController.h"

@interface LearningTaskController ()
@property (nonatomic,assign) int currentPage;

@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation LearningTaskController
static NSString *const reuserID = @"TaskCell";

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.backgroundColor = [UIColor whiteColor];
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf loadMoreData];
        
    }];
}
- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': mywsdxList,'service': PartydeptService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%d}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        [self.dataArray removeAllObjects];
        
        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                TaskModel *model = [TaskModel mj_objectWithKeyValues:dict2];
                [self.dataArray addObject:model];
            }
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)loadMoreData{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': mywsdxList,'service': PartydeptService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%d}'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                TaskModel *model = [TaskModel mj_objectWithKeyValues:dict2];
                [self.dataArray addObject:model];
                
            }
            
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskModel *model = self.dataArray[indexPath.row];
    
    TaskViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserID];

    if (cell == nil) {
        
        cell = [TaskViewCell WithTaskViewCell];
    }
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskModel *model = self.dataArray[indexPath.row];
    TaskDetailController *taskVC = [[TaskDetailController alloc] init];
    taskVC.ID = model.ID;
    [self.navigationController pushViewController:taskVC animated:YES];
}


@end
